<template>
  <div class="departmentList">
    <el-card class="taskList">
      <el-form class="s_form" :inline="true">
        <div class="left">
          <el-form-item>
            <div class="cardName">部门列表</div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="departmentAdd">新增部门</el-button>
          </el-form-item>
        </div>
        <div class="right"></div>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border :height="tabHeight">
        <el-table-column v-for="row in headerList" :key="row.key" :prop="row.key" :min-width="row.width"
          :label="row.label" show-overflow-tooltip header-align="center" align="center"></el-table-column>
        <el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="departmentEdit(scope.row)">编辑部门</el-button>
            <el-button type="text" size="small" @click="departmentDelete(scope.row)">删除部门</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <department-dialog ref="departmentDialog" @refresh="getDataList"/>
  </div>
</template>

<script>
import departmentDialog from './departmentComponent/departmentDialog.vue'
export default {
  components: { departmentDialog },
  data () {
    return {
      dataListLoading: false,
      dataList: [],
      // 资料列表字段
      headerList: [{
        key: 'roleId',
        label: '部门编号',
        width: '150'
      },
      {
        key: 'roleName',
        label: '部门名称',
        width: '150'
      },
      {
        key: 'remark',
        label: '备注',
        width: '100'
      }],
      tabHeight: '600'
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.tabHeight = this.$getClientHeight() - 300 + 'px'
    })
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 新增部门
    departmentAdd () {
      this.$refs.departmentDialog.init()
    },
    // 编辑部门
    departmentEdit (row) {
      this.$refs.departmentDialog.init(row)
    },
    // 删除部门
    departmentDelete (row) {
      this.$confirm('您确认要删除 ' + row.roleName + '吗？', '确认删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.post('/sys/role/delete', [row.roleId]).then(({ data: res }) => {
          let { code, msg } = res
          if (code === 0) {
            this.$message.success('操作成功!')
            this.getDataList()
          }
          if (code !== 0) {
            this.$message.error(msg)
          }
        })
      }).catch(() => {
      })
    },
    // 获取数据
    getDataList () {
      this.dataListLoading = true
      this.$http.get('/sys/role/select').then(({ data: res }) => {
        let { code, msg, list } = res
        console.log(code, msg, list)
        this.dataList = []
        if (code === 0 && list) {
          this.dataList = list && list.length ? list : []
          this.dataListLoading = false
        }
        if (code !== 0) {
          this.$message.error(msg)
        }
        this.dataListLoading = false
      })
    }
  }
}
</script>

<style lang="scss">
.departmentList {
  padding: 20px;
  .taskList {
    width: 100%;
    min-width: 1100px;
    background-color: rgba(255, 255, 255, 1);
    margin-top: 15px;
    .s_form {
      display: flex;
      justify-content: space-between;
      .cardName {
        font-weight: bold;
        font-size: 18px;
      }
    }
  }
}
</style>
